using System;
using System.Collections.Generic;
using System.Text;

namespace GLib.Model.Tools
{
    #region ClassMapProperty
    public class ClassMapProperty
    {
        private string sPropertyName;
        private string sTableField;
        private string sClassField;
        private bool bAllowNull;
        private bool bIsForeignKey;
        private string sForeignKeyTable;
        private string sForeignKeyField;
        private string sForeignKeyProperty;
        private string sForeignKeyPropertyField;


        public string PropertyName
        {
            get { return sPropertyName; }
            set { sPropertyName = value; }
        }

        public string TableField
        {
            get { return sTableField; }
            set { sTableField = value; }
        }

        public string ClassField
        {
            get { return sClassField; }
            set { sClassField = value; }
        }

        public bool AllowNull
        {
            get { return bAllowNull; }
            set { bAllowNull = value; }
        }

        public bool IsForeignKey
        {
            get { return bIsForeignKey; }
            set { bIsForeignKey = value; }
        }

        public string ForeignKeyTable
        {
            get { return sForeignKeyTable; }
            set { sForeignKeyTable = value; }
        }

        public string ForeignKeyField
        {
            get { return sForeignKeyField; }
            set { sForeignKeyField = value; }
        }

        public string ForeignKeyProperty
        {
            get { return sForeignKeyProperty; }
            set { sForeignKeyProperty = value; }
        }

        public string ForeignKeyPropertyField
        {
            get { return sForeignKeyPropertyField; }
            set { sForeignKeyPropertyField = value; }
        }
    }
    #endregion

    #region ClassMap
    public class ClassMap
    {
        private string sTableName;
        private string sClassName;
        private string sPrimaryKey;
        private string sKeyGenerator;
        private string sDefaultProperty;
        private List<ClassMapProperty> lstItems = new List<ClassMapProperty>();
        private List<ClassMapProperty> lstColecoes = new List<ClassMapProperty>();

        public string ClassName
        {
            get { return sClassName; }
            set { sClassName = value; }
        }

        public string TableName
        {
            get { return sTableName; }
            set { sTableName = value; }
        }

        public string PrimaryKey
        {
            get { return sPrimaryKey; }
            set { sPrimaryKey = value; }
        }

        public string KeyGenerator
        {
            get { return sKeyGenerator; }
            set { sKeyGenerator = value; }
        }

        public string DefaultProperty
        {
            get { return sDefaultProperty; }
            set { sDefaultProperty = value; }
        }

        public List<ClassMapProperty> Collections
        {
            get { return lstColecoes; }
            set { lstColecoes = value; }
        }

        public List<ClassMapProperty> Properties
        {
            get { return lstItems; }
            set { lstItems = value; }
        }

        public Dictionary<string, string> Dictionary()
        {
            Dictionary<string, string> dctItems = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
            foreach (ClassMapProperty item in this.Properties)
            {
                dctItems.Add(item.PropertyName, item.TableField.ToUpper());
                if (item.IsForeignKey)
                {
                    dctItems.Add(item.PropertyName + "_" + item.ForeignKeyProperty, item.ForeignKeyTable + "_" + item.ForeignKeyPropertyField);
                }
            }
            return dctItems;
        }

        public ClassMapProperty GetProperty(string name)
        {
            ClassMapProperty oClassMapProperty = null;
            foreach (ClassMapProperty oProperty in lstItems)
            {
                if (oProperty.PropertyName.ToUpper() == name.ToUpper() || oProperty.TableField.ToUpper() == name.ToUpper())
                {
                    oClassMapProperty = oProperty;
                    break;
                }
            }
            return oClassMapProperty;
        }
    }
    #endregion
}
